package cn.bonoon.kernel.events;

import org.springframework.web.servlet.ModelAndView;

import cn.bonoon.kernel.menus.ItemableDefinition;
import cn.bonoon.kernel.menus.privileges.PrivilegeInspector;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.web.models.PanelModel;

/**
 * <pre>
 * 初始化界面的事件，该事件包含：
 * 1.当前菜单{@link #target}
 * 2.当前用户{@link #user}
 * 3.框架界面模型{@link #model}
 * 4.访问操作权限{@link #privilege}
 * 
 * 用于控制器的父类{@link cn.bonoon.kernel.web.controllers.AbstractPanelController AbstractPanelController};
 * 子类在重写父类的onLoad方法时需要把PanelEvent当作onLoad的参数
 * </pre>
 * @author jackson
 *
 */
public class PanelEvent {
	private String vmPath;
	private final ItemableDefinition item;
	private final LogonUser user;
	private final PanelModel model;
	private final PrivilegeInspector privilege;
	
	public PanelEvent(PanelModel model, ItemableDefinition item, LogonUser user, PrivilegeInspector privilege) {
		this.model = model;
		this.item = item;
		this.user = user;
		this.privilege = privilege;
	}

	public void error(Throwable t){
		model.addObject("error", t.getMessage());
		t.printStackTrace();
		vmPath = "error";
	}
	
	public void warn(String msg){
		model.setSysWarnMessage(msg);
	}
	
	public void setVmPath(String vmPath) {
		this.vmPath = vmPath;
	}
	
	public ModelAndView execute() {
		return model.execute(vmPath);
	}

	public PrivilegeInspector getPrivilege() {
		return privilege;
	}
	
	public PanelModel getModel() {
		return model;
	}

	public LogonUser getUser() {
		return user;
	}

	public ItemableDefinition getItem() {
		return item;
	}

//	public IMenu getTarget() {
//		return target;
//	}
}
